import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-01-21
 * Time: 17:28
 */
public class MyLinkedList {
    class Node {
        public int val;
        public Node pre;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

        public Node head; //代表当前链表的头结点的引用
        public Node last; //代表当前链表的头结点的最后一个结点

        //遍历链表
        public void display() {
            Node cur = head;

            while (cur != null) {
                System.out.print(cur.val + " ");
                cur = cur.next;
            }
            System.out.println();
        }

        public boolean contains(int key) {
            Node cur = head;
            while (cur != null) {
                if (cur.val == key) {
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }

        //头插法
        public void addFirst(int data) {
            Node node = new Node(data);
            if(head == null) {
                head = node;
                last = node;
            } else {
                node.next = head;
                head.pre = node;
                head = node;
            }
        }

        //尾插法
        public void addLast(int data) {
            Node node = new Node(data);
            if(head == null) {
                head = node;
                last = node;
            } else {
                last.next = node;
                node.pre = last;
                last = node;
            }
        }


    public void addIndex(int index, int data) throws IndexOutOfException {
        CheckIndex(index);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }

        Node cur = findIndex(index);
        Node node = new Node(data);
        node.next = cur;
        cur.pre.next = node;
        node.pre = cur.pre;
        cur.pre = node;
    }

    private void CheckIndex(int index) {
        if (index < 0 || index > size()) {
            throw new IndexOutOfException("位置不合法");
        }
    }

    private Node findIndex(int index) {
        Node cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
       Node cur = head;
       while(cur != null) {
           if(cur.val == key) {
               if(cur == head) {
                   head = head.next;
                   //只有一个节点
                   if(head != null) {
                       head.pre = null;
                   }
               }else {
                   //中间、尾巴
                  cur.pre.next = cur.next;
                  if(head != last) {
                      cur.next.pre = cur.pre;
                  } else {
                      last = last.pre;
                  }
               }
               return;
           }
               cur = cur.next;
       }

    }


    //删除所有值为key的节点
    public void removeAllKey(int key) {
        Node cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur == head) {
                    head = head.next;
                    //只有一个节点
                    if(head != null) {
                        head.pre = null;
                    }
                }else {
                    //中间、尾巴
                    cur.pre.next = cur.next;
                    if(head != last) {
                        cur.next.pre = cur.pre;
                    } else {
                        last = last.pre;
                    }
                }
                //去掉return
            }
            cur = cur.next;
        }
    }

        //得到单链表的长度
        public int size() {
            Node cur = head;
            int count = 0;
            while(cur != null) {
                count++;
                cur = cur.next;
            }
            return count;
        }

        //清空
        //链表中所有的节点都可以被回收
        public void clear() {
            Node cur = head;
            while(cur != null) {
                Node curNext = cur.next;
                cur.next = null;
                cur.pre = null;
                cur = curNext;
            }
            head = null;
            last = null;
        }
    }


